package thread.simpletest;

import org.junit.Test;

/**
 * @author liweimin
 * @date 2021/4/26 10:43
 * @since
 */
public class SimpleTest2 {

    public static void main(String[] args) {
        Thread thread = new Thread();
        System.out.println("threadName:" + thread.getName());
        System.out.println("threadId:" + thread.getId());
    }

    @Test
    public void test() throws InterruptedException {
        Thread thread_a = new Thread(() -> {
            long begin = System.currentTimeMillis();
            long sum = 0;
            for (int i = 0; i < 100_000; i++) {
                sum += i;
                Thread.yield();//give up 145ms without yield 2ms
            }
            long end = System.currentTimeMillis();
            System.out.println("sub_thread time consumed:" + (end - begin));
        }, "Thread_A");
        thread_a.setPriority(12);
        thread_a.start();

        long begin = System.currentTimeMillis();
        long sum = 0;
        for (int i = 0; i < 100_000; i++) {
            sum += i;
        }
        long end = System.currentTimeMillis();
        System.out.println("main_thread time consumed:" + (end - begin));

        Thread.sleep(10000000);
    }

    @Test
    public void test2() {
        Thread thread_a = new Thread(() -> {
            long begin = System.currentTimeMillis();
            long sum = 0;
            for (int i = 0; i < 100_000; i++) {
                sum += i;
                Thread.yield();//give up 145ms without yield 2ms
            }
            long end = System.currentTimeMillis();
            System.out.println("sub_thread_a time consumed:" + (end - begin));
        }, "Thread_A");
        thread_a.setPriority(8);

        Thread thread_b = new Thread(() -> {
            long begin = System.currentTimeMillis();
            long sum = 0;
            for (int i = 0; i < 100_000; i++) {
                sum += i;
                Thread.yield();//give up 145ms without yield 2ms
            }
            long end = System.currentTimeMillis();
            System.out.println("sub_thread_b time consumed:" + (end - begin));
        }, "Thread_B");

        thread_b.setPriority(2);

        thread_b.start();
        thread_a.start();


        try {
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test3() {

        Thread thread = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("subThread=>" + i++);
            }
        });
        thread.start();


        for (int i = 0; i < 100; i++) {
            System.out.println("mainThread=>" + i++);
        }

        thread.interrupt();//fake block

        try {
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test4() {

        SubThread subThread = new SubThread();
        subThread.start();

        for (int i = 0; i < 100; i++) {
            System.out.println("mainThread=>" + i++);
        }

        subThread.interrupt();//fake block=> giva a tag

        try {
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test5() {
        SubDaemonThread subDaemonThread = new SubDaemonThread();
        subDaemonThread.setDaemon(true);//main线程结束后，子线程由于是守护线程不再执行，设置守护线程应该线程启动前
        subDaemonThread.start();
        for (int i = 0; i < 10; i++) {
            System.out.println("main==" + i);
        }

    }

    @Test
    public void test6() {
        SubThreadNew threadNew = new SubThreadNew();
        System.out.println("当前状态为：" + threadNew.getState());
        threadNew.start();
        System.out.println("当前状态为：" + threadNew.getState());
    }

}

class SubThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (this.isInterrupted()) {
                System.out.println("this thread is interrupted!");
                break;
            }
            System.out.println("sub_thread:=>" + i--);
        }
    }
}

class SubDaemonThread extends Thread {
    @Override
    public void run() {
        super.run();
        while (true) {
            System.out.println("this is a daemon thread--------");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class SubThreadNew extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("当前线程状态为：" + this.getState());

            if (i == 9) {
                System.out.println("当前线程状态为：" + this.getState());
            }
        }
    }
}